#include <QObject>
#include <QMessageBox>
#include <QInputDialog>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include "clientrss.h"
#include "http_request.h"
#include "XMLParser.h"

ClientRSS::ClientRSS(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	connect(this->ui.addButton, SIGNAL(clicked()), this, SLOT(addFeed()));
	connect(this->ui.delButton, SIGNAL(clicked()), this, SLOT(delFeed()));
	connect(this->ui.saveButton, SIGNAL(clicked()), this, SLOT(saveFeed()));
	connect(this->ui.feedList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(setFeed(QListWidgetItem*)));
	connect(this->ui.feedView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(followUrl(QListWidgetItem*)));

	QFile file("myfeeds.txt");
	file.open(QIODevice::ReadOnly);
	QString line;
	line = file.readLine();
	while(line != "")
	{
		line.remove(QChar('\n'), Qt::CaseSensitive);
		addSavedFeed(line);
		line = file.readLine();
	}
	file.close();
}

ClientRSS::~ClientRSS()
{

}

void	ClientRSS::saveFeed()
{
	QFile::remove("myfeeds.txt");
	QFile feeds;
	feeds.setFileName("myfeeds.txt");
	feeds.open(QIODevice::Append);
	QTextStream out(&feeds);
	////////

	int i = 0;
	while (this->ui.feedList->item(i))
	{
		out << this->ui.feedList->item(i)->text() << "\n";
		i++;
	}

	//////////
	feeds.close();
	QMessageBox::information(NULL, "Save", "List Saved !");
}

void	ClientRSS::addSavedFeed(QString path)
{	
	HTTPRequest	request;
	request.setProxy("Y29ldXJfbTo8b25NWFlnZA==");
	if (request.getRSS(path.toStdString()) == 0)
		this->ui.feedList->addItem(path);
	else
		QMessageBox::information(NULL, "Error" , "Invalid Feed Address");
}

void	ClientRSS::addFeed()
{
	QString path = QInputDialog::getText(NULL, "Feed Address", "Please Enter Feed Address");
	
	HTTPRequest	request;
	request.setProxy("Y29ldXJfbTo8b25NWFlnZA==");
	int	add = 0;
	if (request.getRSS(path.toStdString()) == 0)
	{
		int i = 0;
		while (this->ui.feedList->item(i))
		{
			if (this->ui.feedList->item(i)->text() == path)
			{
				QMessageBox::information(NULL, "Error", "Feed already exists");
				add = 1;
				break;
			}
			i++;
		}
		if (add == 0)
		this->ui.feedList->addItem(path);
	}
	else
		QMessageBox::information(NULL, "Error" , "Invalid Feed Address");
}

void	ClientRSS::delFeed()
{
	//if (this->ui.feedList->currentItem() != NULL)
	QListWidgetItem*	item = this->ui.feedList->currentItem();
	delete(item);
}

void	ClientRSS::setFeed(QListWidgetItem* item)
{
	HTTPRequest	request;
	request.setProxy("Y29ldXJfbTo8b25NWFlnZA==");
	request.getRSS(item->text().toStdString());
	XMLParser		parser;
	channel			feed;

	parser.parseFile("tmp.xml");
	feed = parser.getFeed();

	std::list<Item*>::const_iterator	it = feed.getItems().begin();
	std::list<Item*>::const_iterator	ite = feed.getItems().end();

	this->ui.feedView->clear();
	QTextCodec *codec = QTextCodec::codecForName("UTF-8");

	while (it != ite)
	{
		this->ui.feedView->addItem(codec->toUnicode((*it)->getTitle().c_str()));
		this->ui.feedView->addItem(codec->toUnicode((*it)->getDescription().c_str()));
		this->ui.feedView->addItem(codec->toUnicode((*it)->getLink().c_str()));
		this->ui.feedView->addItem("---------------------------------------------------------");
		++it;
	}
}

void	ClientRSS::followUrl(QListWidgetItem* item)
{
	QString url;

	url = item->text();
	if (url.contains("http://", Qt::CaseInsensitive))
		QMessageBox::information(NULL, "URL PopUp", url);
}